var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

//发布评论
router.put('/issue_comment', function (req, res) {
  const body = req.body
  const info = [null, body.comment_content, body.user_id, body.topic_id]
  connection.query(`INSERT INTO topic_comment values(?,?,?,?)`, info, function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      return res.status(200).json({
        message: '评论成功！'
      })
    }
  });
})

router.put('/addCommentNum', function (req, res) {
  const body = req.body
  const topic_commentNum = body.topic_commentNum + 1
  const topicInfo = [topic_commentNum, body.topic_id]
  connection.query(`UPDATE topic_info SET topic_commentNum=? WHERE topic_id=?`, topicInfo, function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      return res.json({
        status: 200,
        message: '评论成功！'
      })
    }
  });

})

module.exports = router;
